//
//  NSObject+method.m
//  测试1
//
//  Created by 李李善 on 2018/10/11.
//  Copyright © 2018年 李李善. All rights reserved.
//

#import "NSObject+method.h"

@implementation NSObject (method)
BOOL classSwizzleInstanceMethod(Class obj, SEL originalSel, SEL swizzleSel)
{
    
    Method originalMethod = class_getInstanceMethod(obj, originalSel);
    Method swizzleMethod = class_getInstanceMethod(obj, swizzleSel);
    
    if (!originalMethod)
    {
        class_addMethod(obj, originalSel, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
        method_setImplementation(swizzleMethod, imp_implementationWithBlock(^(id self, SEL _cmd){ }));
    }
    else
    {
        BOOL didAddMethod = class_addMethod(obj, originalSel, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
        if (didAddMethod)
        {
            class_replaceMethod(obj, swizzleSel, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        }
        else
        {
            method_exchangeImplementations(originalMethod, swizzleMethod);
        }
    }
    
    return YES;
    
}
@end
